package com.formssi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.formssi.mapper.ExcelMapper;
import com.formssi.pojo.UserExcel;
import com.formssi.service.ExcelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * 类名：EasyPoiExcelController
 * 功能：使用easypoi注解进行导入导出
 */
@Controller
@Slf4j
public class EasyPoiExcelController {

    @Autowired
    @Qualifier("excelServiceImpl")
    private ExcelService excelService;

    @RequestMapping("/export")
    public void ExportExcel(HttpServletResponse response) throws IOException {
        List<UserExcel> users = excelService.findAll();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表信息", "用户信息"), UserExcel.class, users);
        response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("用户列表.xls", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }

    /**
     * excel导入
     *
     * @param excelFile
     * @return
     * @throws Exception
     */
    @RequestMapping("/import")
    public String importExcel(MultipartFile excelFile) throws Exception {
        //excel导入
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        params.setNeedVerfiy(true);
//        params.setVerfiyGroup(new Class[]{UserExcel.class});

        List<UserExcel> users = ExcelImportUtil.importExcel(excelFile.getInputStream(), UserExcel.class, params);
//        for (User user : users) {
//            System.out.println(user);
//        }
        excelService.saveAll(users);
        return "redirect:/findAll";
    }

    /**
     * 查询所有
     *
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<UserExcel> users = excelService.findAll();
        model.addAttribute("UserExcels", users);
        return "index";
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}